import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class cxThread extends Thread {
    @Override
    public void run() {
        while (true) {
            System.out.println("cxThread running");
        }
    }
}

class cxThread01 implements Runnable {
    @Override
    public void run() {
        while (true) {
            System.out.println("cxThread01 is running" + cxThread.currentThread().getName());
        }
    }
}

class Pen extends Thread {

    private static AtomicInteger p = new AtomicInteger(1);  // 线程安全的计数器

    @Override
    public void run() {
        while (true) {
            int currentP = p.getAndIncrement();  // 获取并递增
            if (currentP < 51) {
                System.out.println(Thread.currentThread().getName() + "抢到了第" + currentP + "支钢笔");
            } else break;
        }
    }
}

class Pen1 extends Thread {
    private int p = 1;

    @Override
    public void run() {
        penCatch();
    }

    //同步方法
    public synchronized void penCatch() {
        while (true) {
            if (p < 51) {
                System.out.println(Thread.currentThread().getName() + "抢到了第" + p++ + "支钢笔");
            } else break;
        }
    }
}

class Pen2 implements Runnable {
    private int p = 1;
    Lock reentrantLock = new ReentrantLock();

    @Override
    public void run() {
        reentrantLock.lock();
        while (true) {
            if (p < 51) {
                System.out.println(Thread.currentThread().getName() + "抢到了第" + p++ + "支钢笔");
            } else break;
        }
        reentrantLock.unlock();
    }
}

class maxThread implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("MAX");
        }
    }
}

class minThread implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("MIN");
        }
    }
}

class Goods {
    private int goods = 0;

    public int getGoods() {
        return goods;
    }

    public void setGoods(int goods) {
        this.goods = goods;
    }

    public void GoodsAdd() {
        this.goods += 1;
    }

    public void GoodsReduce() {
        this.goods -= 1;
    }
}

class Merchants extends Thread {
    private Goods goods;

    public Merchants(Goods goods) {
        this.goods = goods;
    }

    @Override
    public void run() {
        while (true) {
        synchronized (goods) {
                if (goods.getGoods() != 0) {
                    try {
                        System.out.println("Got the goods! Come grab yours fast!");
                        goods.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    goods.notify();
                }
                goods.GoodsAdd();
                goods.notify();
            }
        }
    }
}

class customer extends Thread {
    private Goods goods;

    public customer(Goods goods) {
        this.goods = goods;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (goods) {

                if (goods.getGoods() == 0) {
                    try {
                        System.out.println("haven`t goods ,get some goods hurry up!!");
                        goods.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                } else {

                    System.out.println("i buy 1,now !");
                    goods.GoodsReduce();
                }
                goods.notify();
            }
        }
    }
}